package com.rc.web.utils;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * 解析数据为音频，返回给前端
 */
public class AudioDecoder {

    public static byte[] pcmToWav(byte[] pcmData) throws IOException {
        ByteArrayOutputStream wavStream = new ByteArrayOutputStream();
        AudioFormat format = new AudioFormat(24000, 16, 1, true, false);
        AudioInputStream audioInputStream = new AudioInputStream(new ByteArrayInputStream(pcmData), format, pcmData.length);
        AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, wavStream);
        return wavStream.toByteArray();
    }
}